package com.crm.util;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Value;

import com.crm.model.BaseModel;
import com.crm.model.SysUser;

public class ExcelUtil {

	/**
	 * 
	 * @Description : TODO
	 * @author zealot
	 * @param
	 * @return void
	 * @version 创建时间 : 2016年11月14日 下午3:04:06
	 * @throws IOException 
	 * @thorws
	 */
	public static HSSFWorkbook productExcel(List<? extends BaseModel> list,Class T) throws IOException {
		@SuppressWarnings("resource")
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("用户导出表");
		HSSFRow row = sheet.createRow((int) 0);
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		String[] str = null;
		String[][] valList = null;
		try {
			str = reflectKey(T.newInstance());
		} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
				| InvocationTargetException | InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			valList = reflectValueList(T.newInstance(),list);
		} catch (SecurityException | NoSuchMethodException | IllegalArgumentException | IllegalAccessException
				| InvocationTargetException | InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		/**
		 * 设置表头
		 */
		HSSFCell cell = null;
		for(int i=0;i<str.length;i++){
			cell = row.createCell(i);
			cell.setCellValue(str[i]);
			cell.setCellStyle(style);
		}
		
		/**
		 * 表格内容
		 */
		for (int i = 0; i < valList.length; i++) {
			row = sheet.createRow((int) i + 1);
			for(int k=0;k<valList[i].length;k++){
				row.createCell(k).setCellValue(valList[i][k]);
			}
		}
		
//		FileOutputStream out =new FileOutputStream("/Users/zhaoyulong/Documents/workspace:11.xls");
//		wb.write(out); 
//		out.close();
		return wb;
	}

	public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException,
			IllegalAccessException, InvocationTargetException, InstantiationException, IOException {
		SysUser s = new SysUser();
		s.setUserId("111");
		s.setAccount("xxx");
		SysUser s2 = new SysUser();
		s2.setUserId("222");
		s2.setAccount("yyy");
		List<SysUser> list = new ArrayList<SysUser>();
		list.add(s2);
		list.add(s);
		ExcelUtil.productExcel(list,SysUser.class);
	}

	
	/**
	 * 
	 * @Description : 获取实体类中参数集合
	 * @author zealot
	 * @param @param model
	 * @param @param list
	 * @param @return
	 * @param @throws SecurityException
	 * @param @throws NoSuchMethodException
	 * @param @throws IllegalArgumentException
	 * @param @throws IllegalAccessException
	 * @param @throws InvocationTargetException
	 * @param @throws InstantiationException
	 * @return String[][]
	 * @version 创建时间 : 2016年11月14日 下午4:30:37
	 * @thorws
	 */
	public static String[][] reflectValueList(Object model,List<? extends BaseModel> list) throws SecurityException, NoSuchMethodException, IllegalArgumentException,
			IllegalAccessException, InvocationTargetException, InstantiationException {
		Field[] field = model.getClass().getDeclaredFields();
		String[] modelType = new String[field.length];
		String[][] str = new String[list.size()][field.length];
		for(int k=0;k<list.size();k++){
			model = list.get(k);
			for (int i = 0; i < field.length; i++) {
				Value col =  field[i].getAnnotation(Value.class);
				if(col!=null){
					String name = field[i].getName();
					String type = field[i].getGenericType().toString();
					modelType[i] = type;
					field[i].setAccessible(true);
					name = name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toUpperCase());
					Method main = model.getClass().getMethod("get" + name);
					String v =  main.invoke(model)==null?"":main.invoke(model).toString();
					str[k][i] = v;
				}
			}
		}
		return str;
	}

	/**
	 * 
	 * @Description : 获取实体类属性名称为excel做准备
	 * @author zealot
	 * @param @param model
	 * @param @throws NoSuchMethodException
	 * @param @throws SecurityException
	 * @param @throws IllegalAccessException
	 * @param @throws IllegalArgumentException
	 * @param @throws InvocationTargetException
	 * @return String[]
	 * @version 创建时间 : 2016年11月14日 下午3:53:38
	 * @thorws
	 */
	public static String[] reflectKey(Object model) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		Field[] field = model.getClass().getDeclaredFields();
		String[] modelName = new String[field.length];
		String[] str = new String[field.length];
		for (int i = 0; i < field.length; i++) {
			Value col =  field[i].getAnnotation(Value.class);
			if(col!=null){
				String name = col.value();
				modelName[i] = name;
				str[i] = name;
			}
		}
		return str;
	}
}
